<?php
require_once("unit.php");
require_once ("util.php");
require_once ("sql.php");

class ManageUnit {
	public  function addUnit($companyId,$unit,$unitShort){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::UNIT_ADD;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('ssd',$unit,$unitShort,$companyId);
		$status=$stmt->execute();
		$conn->commit();
		$conn->close();
		return $status;
	}
	public  function updateUnit($id,$unit,$unitShort){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::UNIT_UPDATE;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('ssd',$unit,$unitShort,$id);
		$status=$stmt->execute();
		$conn->commit();
		$conn->close();
		return $status;
	}
	public function deleteUnit($id){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::UNIT_DELETE;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('d',$id);
		$status=$stmt->execute();
		$conn->commit();
		$conn->close();
		return $status;
	}
	public function getUnitList($unitId=null,$canViewAllUnits=false){
		$util=new DBUtil();
		$conn=$util->getConnection();
		if($canViewAllUnits){
			$sql = SQLQuery::UNIT_ADMIN_LIST;
			$stmt = $conn->prepare( $sql);
		}else{
			$sql = SQLQuery::UNIT_USER_LIST;
			$stmt = $conn->prepare( $sql);
			$stmt->bind_param('d',$unitId);
		}
		$status=$stmt->execute();
		$result = $stmt->get_result ();
		$units=array();
		if ($result->num_rows > 0) {
			while ( $row = $result->fetch_array () ) {
				$unit=new Unit();
				$unit->setId($row ["ID"]);
				$unit->setUnit($row ["UNIT"]);
				$unit->setUnitShort($row ["UNIT_SHORT"]);
				$unit->setCompanyId($row ["COMPANY_ID"]);
				$unit->setCompanyName($row ["COMPANY"]);
				array_push($units,$unit);
			}
		}
		$conn->close();
		return $units;
	}
	public function getUnit($unitId){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::UNIT_FIND;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('d',$unitId);
		$status=$stmt->execute();
		$result = $stmt->get_result ();
		$unit=null;
		if ($result->num_rows > 0) {
			while ( $row = $result->fetch_array () ) {
				$unit=new Unit();
				$unit->setId($row ["ID"]);
				$unit->setUnit($row ["UNIT"]);
				$unit->setUnitShort($row ["UNIT_SHORT"]);
				$unit->setCompanyId($row ["COMPANY_ID"]);
				$unit->setCompanyName($row ["COMPANY"]);
			}
		}
		$conn->close();
		return $unit;
	}
	public function getCompanyByUnitId($unitId){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::COMPANY_FIND_BY_UNIT;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('d',$unitId);
		$status=$stmt->execute();
		$result = $stmt->get_result ();
		$company=null;
		if ($result->num_rows > 0) {
			while ( $row = $result->fetch_array () ) {
				$company=new Company();
				$company->setId($row ["ID"]);
				$company->setCompany($row ["COMPANY"]);
				$company->setCompanyShort($row ["COMPANY_SHORT"]);
			}
		}
		$conn->close();
		return $company;
	}	
}
?>